package com.shujia.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

public class Code03ConfigOperator {
    FileSystem fileSystem;


    @Before
    public void getCon(){

        /*
            对于添加配置参数可以使用Configuration 也可以在resources中添加对应的配置文件
                其中代码优先级高于配置文件
         */

        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://master:9000");
        conf.set("dfs.replication","2");
        System.setProperty("HADOOP_USER_NAME","root");
        try {
            fileSystem =  FileSystem.get(conf);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }


    @Test
    public void putData(){

        Path src = new Path("./data/hdfs/students.txt"); // 本地路径
        Path dst = new Path("/api/students.txt");


        try {
            fileSystem.copyFromLocalFile(src,dst);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }



    @After
    public void closeCon(){
        try {
            fileSystem.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
